package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.MyFavoriteTopic;
import mamikeji.liaowa.bean.MyForumBean;
import mamikeji.liaowa.ui.activity.circle.PreviewCircleImgActivity;

/**
 * Created by 叶寅 on 2017/9/24.
 */

public class TopicRecyclerViewAdapter extends RecyclerView.Adapter {

    Context mContext;
    OnItemClickListener mOnItemClickListener;
    List<MyForumBean.DataEntity> mData;

    public void refreshDate(List<MyFavoriteTopic.DataMyFavoriteTopicList> data){
//        this.mData.clear();
/*        this.mData.addAll(data);
        notifyDataSetChanged();*/
    }

    public TopicRecyclerViewAdapter(Context context, List<MyForumBean.DataEntity> data) {
        this.mContext = context;
        this.mData = data;
    }

    public void setItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);

        void onZanClick(int position);

        void onGuanZhuClick(int position);

        void onMSGClick(int position);

        void onCanelCollection(int position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_my_favorite_topic, null);
        return new TopicViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

//        //关注点击事件
//        ((TopicViewHolder) holder).guanZhuViewGroup.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mOnItemClickListener.onGuanZhuClick(position);
//            }
//        });
//        //点赞点击事件
//        ((TopicViewHolder) holder).dianZuanImage.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mOnItemClickListener.onZanClick(position);
//            }
//        });

        ((TopicViewHolder) holder).setData(position);

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class TopicViewHolder extends RecyclerView.ViewHolder {
        private View mView;
        private ImageView userImage;
        private ImageView userIdentity;
        private TextView userName;
        private TextView userRecommend;
        private TextView topicTitle;
        private TextView topicContent;
        private ImageView photoConten;
        private TextView browseNum;
        private ImageView browseImage;
        private Button mDeleteBtn;
        private LinearLayout linearLayout_my_collection_forum_item;


        /*private ImageView dianZuanImage;
        private TextView dianZuanNumber;
        private ImageView messageImage;
        private TextView MessageNum;
        private TextView beforeTime;
        private LinearLayout guanZhuViewGroup;
        private ImageView guanZhuImage;
        private TextView guanZhuTv;*/

        public TopicViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            linearLayout_my_collection_forum_item = (LinearLayout) itemView.findViewById(R.id.linearLayout_my_collection_forum_item);

            userImage = (ImageView) itemView.findViewById(R.id.imageView_my_forum_titleimg);

            userIdentity = (ImageView) itemView.findViewById(R.id.imageView_my_forum_identity);

            userName = (TextView) itemView.findViewById(R.id.textView_my_forum_nickname);

            userRecommend = (TextView) itemView.findViewById(R.id.textView_my_forum_position);

            topicTitle = (TextView) itemView.findViewById(R.id.textView_my_forum_title);

            topicContent = (TextView) itemView.findViewById(R.id.textView_my_forum_content);

            photoConten = (ImageView) itemView.findViewById(R.id.imageView_my_forum_img);

            browseImage = (ImageView) itemView.findViewById(R.id.imageView80);
            browseNum = (TextView) itemView.findViewById(R.id.textView_my_forum_browsenum);
            mDeleteBtn = (Button) itemView.findViewById(R.id.btnDelete);


            /*dianZuanImage = (ImageView) itemView.findViewById(R.id.imageView_my_forum_zannum);
            dianZuanNumber = (TextView) itemView.findViewById(R.id.textView_my_forum_zannum);

            messageImage = (ImageView) itemView.findViewById(R.id.imageView_my_forum_img_recommendnum);
            MessageNum = (TextView) itemView.findViewById(R.id.textView_my_forum_recommend);

            guanZhuViewGroup = (LinearLayout) itemView.findViewById(R.id.linearLayout_my_forum_img_guanzhu);
            guanZhuImage = (ImageView) itemView.findViewById(R.id.imageView_my_forum_guanzhu);
            guanZhuTv = (TextView) itemView.findViewById(R.id.textView_my_forum_guanzhu);

            beforeTime = (TextView) itemView.findViewById(R.id.textView_my_forum_creattime);*/




        }

        /**
         * is_follow (integer, optional): 是否关注 ,
         * is_praise (integer, optional): 是否点赞 ,
         * browse_total (integer, optional): 浏览数 ,
         * is_collection (integer, optional): 是否收藏 ,
         * message_total (integer, optional): 留言数 ,
         * praise_total (integer, optional): 点赞数 ,
         * certification (string, optional): 认证用户名 ,
         * topic_id (integer, optional): 话题id ,
         * topic_title (string, optional): 话题标题 ,
         * topic_content (string, optional): 话题内容 ,
         * nickname (string, optional): 名称 ,
         * usercode (string, optional): ,
         * photo (string, optional): 照片 ,
         * publishdate (string, optional): 时间 ,
         * topic_content_type (integer, optional): 话题内容类型 ,
         * topic_image (string, optional):
         *
         * @param position
         */

        public void setData(final int position) {

            //MessageNum.setText(mData.get(position).getMessage_total() + "");
            //beforeTime.setText(mistiming(mData.get(position).getPublishdate()));
            //Glide.with(mContext).load(mData.get(position).getTopic_image()).centerCrop().into(photoConten);
            //Glide.with(mContext).load(mData.get(position).getPhoto()).centerCrop().into();


            browseNum.setText(mData.get(position).getBrowse_total() + "");

            topicTitle.setText(mData.get(position).getTopic_title());

            topicContent.setText(mData.get(position).getTopic_content());
            if (mData.get(position).getTopic_content().length() >= 20) {
                topicContent.setLines(2);
                topicContent.setEllipsize(TextUtils.TruncateAt.END);
            }

            userName.setText(mData.get(position).getNickname());

            userImage.setImageURI(Uri.parse(mData.get(position).getPhoto()));



            String[] split2 = new String[]{};
            if (!mData.get(position).getTopic_image().equals("") && !mData.get(position).getTopic_image().equals("null") && mData.get(position).getTopic_image() != null) {
                split2 = mData.get(position).getTopic_image().split(",");
                photoConten.setVisibility(View.VISIBLE);
                Glide
                        .with(mContext)
                        .load(split2[0] + "?from=singlemessage")
                        .error(R.drawable.img_dongtaitu)
                        .into(photoConten);
            } else {
                photoConten.setVisibility(View.GONE);
            }

            userRecommend.setText(mData.get(position).getCertification());
            userIdentity.setBackgroundResource(mData.get(position).getCertification().equals("") ? R.drawable.icon_renzheng_n : R.drawable.icon_renzheng_h);

            //查看大图
            final String[] finalSplit = split2;
            photoConten.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ArrayList<String> stringList = new ArrayList<String>();
                    stringList.add(finalSplit[0]);
                    mContext.startActivity(
                            new Intent(mContext, PreviewCircleImgActivity.class)
                                    .putStringArrayListExtra("urlList", stringList)
                                    .putExtra("position", 1));
                }
            });

           mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(position);
                }
            });
            linearLayout_my_collection_forum_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(position);
                }
            });

            //头像点击事件
            userImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onMSGClick(position);
                }
            });

            mDeleteBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onCanelCollection(position);
                }
            });
        }


        //判断时间差：新闻发布时间与当前系统时间的差值：如果小于60分钟换算成分钟；大于60分钟，换算成小时；大于24小时，换算成天
        /*public String mistiming(String time) {
            String date1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());

            SimpleDateFormat CurrentTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

            Date beginTime = null;
            try {
                beginTime = CurrentTime.parse(date1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Date endTime = null;
            try {
                endTime = CurrentTime.parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //进行判断
            Long shijianCha = beginTime.getTime() - endTime.getTime();
            int count = 0;
            if (shijianCha > 24 * 60 * 60 * 1000) {
                count = (int) (shijianCha / (24 * 60 * 60 * 1000));
                return count + "天前";
            } else if (shijianCha > 60 * 60 * 1000) {
                count = (int) (shijianCha / (60 * 60 * 1000));
                return count + "小时前";
            } else {
                count = (int) (shijianCha / (60 * 1000));
                return count + "分钟前";
            }
        }*/


    }


}
